package com.vimi8.ebb.auth.service;

import com.vimi8.ebb.auth.model.DBClientDetails;
import org.apache.ibatis.annotations.*;

/**
 * Created by xuybin on 2016/9/8.
 *
 */
@Mapper
public interface DBClientDetailsService {
   
    @Select("SELECT client_id AS clientId,client_secret AS clientSecret,grant_type AS grantTypes,redirect_uri AS redirectUri,access_token_expires_in AS accessTokenExpiresIn,refresh_token_expires_in AS refreshTokenExpiresIn, " +
            " auto_resources AS autoResources,manu_resources AS manuResources, manu_scopes AS manuScopes,auto_scopes AS autoScopes,allow_users_to_join AS allowUsersToJoin FROM oauth_client WHERE client_id=#{client_id} AND is_enabled=1")
    public DBClientDetails getClientDetailsByClientId(String client_id);

    @Select("SELECT COUNT(*) FROM `oauth_client_user` WHERE client_id=#{client_id} AND user_id IN (SELECT user_id FROM `oauth_user` WHERE phone_number = #{login_name} OR  id_card = #{login_name} OR  user_name=#{login_name} )")
    public int getCountClientUser(@Param("client_id") String client_id, @Param("login_name") String login_name);

    @Select("SELECT default_client_id FROM `oauth_user` WHERE phone_number = #{login_name} OR  id_card = #{login_name} OR  user_name=#{login_name}")
    public String getDefaultClient(@Param("login_name") String login_name);

    @Insert("insert into `oauth_client` (`client_id`, `client_secret`, `grant_type`, `redirect_uri`, `allow_users_to_join`, `auto_resources`, `auto_scopes`, `manu_resources`, `manu_scopes`) " +
            "values(#{clientId},#{clientSecret},#{grantTypes},#{redirectUri},#{allowUsersToJoin},#{autoResources},#{autoScopes},#{manuResources},#{manuScopes})")
    public int insertClientDetails(DBClientDetails dbClientDetails);

    /**
     * 冻结员工
     * @param openId
     * @return
     */
    @Update("update oauth_client_user as u,organization_emp as emp set emp.`is_freeze` = #{freezesStatus} where u.`user_id` = emp.`user_id`" +
            " and u.`open_id` =#{openId} and u.client_id=#{orgId}")
    public int freezeEmployee(@Param("openId") String openId,@Param("orgId") String orgId,@Param("freezesStatus")String freezesStatus);

    /**
     * 移除员工
     * @param userId
     * @return
     */
    @Delete("delete u,e from oauth_client_user as u,organization_emp as e where e.user_id = u.user_id and u.user_id = #{userId}" +
            " and u.client_id=#{orgId}")
    public int deleteEmployee(@Param("userId") String userId,@Param("orgId") String orgId);

}
